﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class UserAlbumPhotoController : DefaultController
	{
		public UserAlbumPhotoController(
			IUserAlbumService userAlbumService, 
			IUserAlbumPhotoService userAlbumPhotoService,
			IAttachmentStoreService attachmentStoreService)
		{
			this.UserAlbumService = userAlbumService;
			this.UserAlbumPhotoService = userAlbumPhotoService;
			this.AttachmentStoreService = attachmentStoreService;
		}

		public IUserAlbumService UserAlbumService
		{
			get;
			private set;
		}

		public IUserAlbumPhotoService UserAlbumPhotoService
		{
			get;
			private set;
		}

		public IAttachmentStoreService AttachmentStoreService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Index(int id, int? page)
		{
			var album = UserAlbumService.GetById(id);

			if (album == null)
			{
				return Error(Resources.Messages.UserAlbumNotFound);
			}

			album.Photos = UserAlbumPhotoService.GetPaged(new UserAlbumPhotoSpecification
			{
				AlbumId = album.Id,
				Page = page,
				Limit = Settings.Users.AlbumPhotoPageLimit
			});

			return album.CanUpdate || album.CanUpdateAny ? View(Views.Index, album) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Detail(int id, string slug)
		{
			var photo = UserAlbumPhotoService.GetById(id);

			if (photo == null)
			{
				return Error(Resources.Messages.UserAlbumPhotoNotFound);
			}

			UserAlbumService.Determine(photo.Album);

			return photo.Album.IsContact ? View(Views.Detail, photo) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Create(int? id)
		{
			var photo = UserAlbumPhotoService.Create();

			return photo.CanCreate ? View(Views.Create, new UserAlbumPhotoCreate(id)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Create(UserAlbumPhotoCreate value)
		{
			try
			{
				var photo = UserAlbumPhotoService.Create();

				if (!photo.CanCreate)
				{
					return NotAuthorized();
				}

				value.Attachment.Source = Request.Files[0];

				if (AttachmentStoreService.Process(value.Attachments, value.Attachment))
				{
					return View(Views.Create, value);
				}

				UserAlbumPhotoService.Insert(value);

				return RedirectToRoute(Routes.UserAlbumPhotoIndex, new { id = value.AlbumId, page = 1 });
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Update(int id, UserAlbumPhotoUpdate[] values)
		{
			try
			{
				UserAlbumPhotoService.Update(values);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return RedirectToRoute(Routes.UserAlbumPhotoIndex, new { id = id, page = 1});
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Delete(int id)
		{
			var photo = UserAlbumPhotoService.GetById(id);

			if (photo == null)
			{
				return Error(Resources.Messages.UserAlbumNotFound);
			}

			return photo.CanDelete || photo.CanDeleteAny ? View(Views.Delete, new UserAlbumPhotoDelete(photo)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Delete(UserAlbumPhotoDelete value)
		{
			try
			{
				var photo = UserAlbumPhotoService.GetById(value.Id);

				if (photo == null)
				{
					return Error(Resources.Messages.UserAlbumPhotoNotFound);
				}

				if (!photo.CanDelete && !photo.CanDeleteAny)
				{
					return NotAuthorized();
				}

				UserAlbumPhotoService.Delete(photo);

				return RedirectToRoute(Routes.UserAlbumIndex);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Delete, value);
		}
	}
}